package main.www.wonderfuldata.PreparedStatement;

import main.www.wonderfuldata.com.StatementTest;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.Scanner;
import java.util.SimpleTimeZone;
import java.util.logging.SimpleFormatter;

public class testConnectionUpdate {

    //测试insert插入数据

    public static void main(String[] args) throws Exception {
        TestInsert();


    }
    public static void TestInsert() throws Exception {

        //连接部分
        InputStream is = testConnectionUpdate.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        // 2.读取配置信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        // 3.加载驱动

        Class.forName(driverClass);

        // 4.获取连接
        Connection conn = DriverManager.getConnection(url, user, password);


        //创造sql语句,预编译sql语句
        String sql = "insert into student01(name,email,brith)values(?,?,?)";//?占位符
        Scanner in = new Scanner(System.in);
        String name = in.next();
        String email = in.next();
        String brith = in.next();

        PreparedStatement pr =  conn.prepareStatement(sql);


        //填充占位符
        pr.setNString(1,name);
        pr.setNString(2,email);

        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
         java.util.Date  date  = null;
         date =   sdf.parse(brith);
         pr.setDate(3, new Date(date.getTime()));
        //执行
        pr.execute();
        JDBCUtils.closeResource(conn,pr);
    }
}
